﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.DataMining
{
    /// <summary>
    /// This class provides the export of numerical datas into a CSV file.
    /// </summary>
    [Serializable]
    public class CSVDataWriter
    {
        /// <summary>
        /// The specified data to write.
        /// </summary>
        private double[,] data;

        /// <summary>
        /// The file, to which the data should be written.
        /// </summary>
        private FileStream exportFile;

        /// <summary>
        /// The element separator to use.
        /// </summary>
        private char elementSeparator;

        /// <summary>
        /// The number format information for the numbers.
        /// </summary>
        private NumberFormatInfo numberFormatInfo;

        /// <summary>
        /// Initializes a new instance of the <see cref="CSVDataWriter"/> class.
        /// </summary>
        /// <param name="exportFile">The file, to which the data should be written.</param>
        /// <param name="data">The specified data to write.</param>
        /// <param name="elementSeparator">The element separator to use.</param>
        /// <param name="numberFormatInfo">The number format information for the numbers.</param>
        public CSVDataWriter(FileStream exportFile, double[,] data, char elementSeparator,
                             NumberFormatInfo numberFormatInfo)
        {
            this.data = data;
            this.exportFile = exportFile;
            this.numberFormatInfo = numberFormatInfo;
            this.elementSeparator = elementSeparator;
        }

        /// <summary>
        /// Gets or sets the specified data to write.
        /// </summary>
        /// <value>The specified data to write.</value>
        public double[,] Data
        {
            get { return data; }
            set { data = value; }
        }

        /// <summary>
        /// Gets or sets the file, to which the data should be written.
        /// </summary>
        /// <value>The file, to which the data should be written.</value>
        public FileStream ExportFile
        {
            get { return exportFile; }
            set { exportFile = value; }
        }

        /// <summary>
        /// Gets or sets the element separator to use.
        /// </summary>
        /// <value>The element separator to use.</value>
        public char ElementSeparator
        {
            get { return elementSeparator; }
            set { elementSeparator = value; }
        }

        /// <summary>
        /// Gets or sets the number format information for the numbers.
        /// </summary>
        /// <value>The number format information for the numbers.</value>
        public NumberFormatInfo NumberFormatInfo
        {
            get { return numberFormatInfo; }
            set { numberFormatInfo = value; }
        }

        /// <summary>
        /// Exports the specified data into the specified file.
        /// </summary>
        public void ExportData()
        {
            StreamWriter writer = new StreamWriter(this.exportFile);

            for (int i = 0; i < this.data.GetLength(0); i++)
            {
                String tempuri = "";

                for (int j = 0; j < this.data.GetLength(1); j++)
                {
                    tempuri += this.data[i, j].ToString(this.numberFormatInfo) + this.elementSeparator;
                }

                writer.WriteLine(tempuri);
            }

            writer.Close();
        }
    }
}